/**
 * 
 */
package com.wbcs.jbsf.modal.abs;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wbcs.jbsf.format.IFormat;

/**
 * 行数据模型
 * @author hawkfly
 */
public abstract class AbsRowModal
{
    private Log log = LogFactory.getLog(AbsRowModal.class);
    private Map<String, Object> datas;
    private ResultSet rs;
    private String[] colnames;
    
    private List<IFormat> formatsall = new ArrayList<IFormat>();
    private Map<String[], IFormat[]> formatcolnames = new HashMap<String[], IFormat[]>();
    
    public AbsRowModal(){
        datas = new LinkedHashMap<String, Object>();
    }
    
    public AbsRowModal(Map<String, Object> rowmodal){
        datas = rowmodal;
    }
    
    public AbsRowModal addCol(String colname, Object colvalue){
        this.datas.put(colname,colvalue);
        return this;
    }
    
    public AbsRowModal addRow(Map<String, Object> rowmap){
        if(datas == null){
            datas = new LinkedHashMap<String, Object>();
        }
        datas.putAll(rowmap);
        return this;
    }
    /**
     * 在注入resultset后使用
     * @param colname 列明
     * @return AbsRowModal
     */
    public AbsRowModal addCols(String[] colnames){
        if(rs == null){
            String errormsg = "在使用addCol(colname)方法前必须设定结果集";
            log.error(errormsg,new Exception(errormsg));
            return this;
        }
        try
        {
            for(int i=0;i<colnames.length;i++)
            {
                addCol(colnames[i],rs.getObject(colnames[i]));
            }
        }catch(SQLException e)
        {
            e.printStackTrace();
        }
        return this;
    }
    
    public AbsRowModal add(){
        if(rs == null || colnames == null){
            String errormsg = "在使用add()方法前必须设定结果集并制定添加列，请调用setResultSet和setColnames方法";
            log.error(errormsg,new Exception(errormsg));
            return this;
        }
        
        addColIncludeFormats();
        
        return this;
    }
    
    public AbsRowModal setColnames(String[] colnames){
        this.colnames = colnames;
        return this;
    }
    
    /**
     * 泛化格式方法即对指定的所有列均实行格式化
     * @param format 实现IFormat的格式化接口
     * @return AbsRowModal实例
     */
    public AbsRowModal setColFormat(IFormat format){
        formatsall.add(format);
        return this;
    }
    /**
     * 泛化格式方法即对指定的所有列均实行格式化
     * @param colnames 要格式化的列名称
     * @param format 实现IFormat的格式化接口
     * @return AbsRowModal实例
     */  
    public AbsRowModal setColFormat(String[] colnames, IFormat[] formats){
        formatcolnames.put(colnames,formats);
        return this;
    }
    
    public AbsRowModal setResultSet(ResultSet rs){
        this.rs = rs;
        return this;
    }
    
    public Object getColValue(String key){
        return datas.get(key);
    }
    
    public Map<String, Object> toMap(){
        return datas;
    }
    
    private void addColIncludeFormats(){
        try
        {
            for(int i=0;i<colnames.length;i++)
            {
               String colname = colnames[i];
               String aftformatval = rs.getString(colname);
               Iterator<String[]> it = formatcolnames.keySet().iterator();
               while(it.hasNext()){
                   String[] assigncolnames = it.next();
                   for(String item:assigncolnames)
                   {
                    if(colname.equalsIgnoreCase(item))
                    {
                        for(IFormat formatitem:formatcolnames.get(assigncolnames))
                        {
                            aftformatval = formatitem.StrFomat(aftformatval);
                        }
                        //aftformatval = formatcolnames.get(assigncolnames).StrFomat(rs.getString(colname));
                    }
                   }
               }
               for(IFormat formatsallitem:formatsall)//执行泛化格式处理
               {
                   aftformatval = formatsallitem.StrFomat(aftformatval);
               }
               addCol(colname,aftformatval);
            }
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            colnames = null;
            formatsall.clear();
            formatcolnames.clear();
        }
    }
}

